<template>
    <el-dialog title="您遇到了什么问题？" :visible.sync="contactVisible" :before-close="handleClose" style="text-align: left;">
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
            <el-form-item label="问题标题" prop="title">
                <el-input v-model="form.title" placeholder="请输入问题标题"></el-input>
            </el-form-item>
            <el-form-item label="问题描述" prop="desc">
                <el-input type="textarea" v-model="form.desc" placeholder="请输入问题描述"></el-input>
            </el-form-item>
            <el-form-item label="截图">
                <el-upload
                    action="https://jsonplaceholder.typicode.com/posts/"
                    list-type="picture-card"
                    :on-preview="handlePictureCardPreview"
                    :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item label="联系方式" prop="contact">
                <el-input v-model="form.contact" placeholder="请输入联系方式"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button @click="resetContent">清 空</el-button>
            <el-button type="primary" @click="submitContact">提 交</el-button>
        </div>
    </el-dialog>
</template>

<script>

    import {baseUtil} from '@/util/BaseUtil';

    export default {
        name: "el-contact-me",
        props: {
            contactVisible: {
                type: Boolean,
                default: true
            },
            module : {
                type: String,
                default: "系统"
            }
        },
        data() {
            return {
                baseUtil :baseUtil,
                childContactVisible: this.contactVisible,
                form: {
                    title: '',
                    desc: '',
                    contact: ''
                },
                rules: {
                    title: [
                        {required: true, message: '请输入问题标题', trigger: 'blur'},
                        {min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur'}
                    ],
                    desc: [
                        {required: true, message: '请输入问题描述', trigger: 'blur'},
                        { min: 1, max: 255, message: '长度在 1 到 255 个字符', trigger: 'blur' }
                    ],
                    contact: [
                        { required: true, message: '联系方式不能为空'}
                    ]
                },
                formLabelWidth: '120px',

                dialogImageUrl: '',
                dialogVisible: false
            }
        },
        methods: {
            // 清空内容
            resetContent() {
                this.form.title = '';
                this.form.desc = '';
                this.form.contact = '';
            },
            // 提交数据
            submitContact() {
                this.baseUtil.ajaxPost(this, '/feedback/addFeedback', {
                    feedbackTitle : this.form.title,
                    feedbackContent : this.form.desc,
                    feedbackContact : this.form.contact,
                    feedbackModule : this.module
                }, (response) => {
                    this.$notify({
                        title: response.message,
                        type: 'success'
                    });
                    this.resetContent();
                    this.handleClose();
                });
            },
            handleClose(){
                // this.resetContent();
                this.$emit('overContact', {});
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            }
        },
        created() {
            // 清空内容
            this.resetContent();
        }
    }
</script>

<style scoped>

</style>
